// 收藏功能实现
function toggleFavorite(event, toolId) {
  event.preventDefault();
  const favorites = JSON.parse(localStorage.getItem('toolFavorites') || '[]');
  const index = favorites.indexOf(toolId);
  const favoriteBtn = event.currentTarget;
  const emptyStarIcon = favoriteBtn.querySelector('.star-empty');
  const filledStarIcon = favoriteBtn.querySelector('.star-filled');
  const toolItem = favoriteBtn.closest('.tool-item');
  
  // 检查当前是否在"常用工具"分类下
  const activeCategory = document.querySelector('.category-badge.active');
  const isCommonCategory = activeCategory && activeCategory.dataset.category === 'common';

  if (index === -1) {
    // 添加到收藏
    favorites.push(toolId);
    // 切换星标显示状态
    if (emptyStarIcon && filledStarIcon) {
      emptyStarIcon.style.display = 'none';
      filledStarIcon.style.display = 'inline';
    }
  } else {
    // 从收藏中移除
    favorites.splice(index, 1);
    // 切换星标显示状态
    if (emptyStarIcon && filledStarIcon) {
      emptyStarIcon.style.display = 'inline';
      filledStarIcon.style.display = 'none';
    }
    
    // 如果在常用工具分类下，需要隐藏被取消收藏的工具
    if (isCommonCategory && toolItem) {
      toolItem.style.display = 'none';
    }
  }

  localStorage.setItem('toolFavorites', JSON.stringify(favorites));
}

// 初始化收藏状态
document.addEventListener('DOMContentLoaded', () => {
  const favorites = JSON.parse(localStorage.getItem('toolFavorites') || '[]');
  
  // 更新收藏图标状态
  favorites.forEach(id => {
    const favoriteBtn = document.querySelector(`[data-tool-id='${id}']`);
    if (favoriteBtn) {
      const emptyStarIcon = favoriteBtn.querySelector('.star-empty');
      const filledStarIcon = favoriteBtn.querySelector('.star-filled');
      if (emptyStarIcon && filledStarIcon) {
        emptyStarIcon.style.display = 'none';
        filledStarIcon.style.display = 'inline';
      }
    }
  });
  
  // 如果当前是常用工具分类，确保收藏的工具显示
  const activeCategory = document.querySelector('.category-badge.active');
  if (activeCategory && activeCategory.dataset.category === 'common') {
    const toolItems = document.querySelectorAll('.tool-item');
    toolItems.forEach(item => {
      const favoriteBtn = item.querySelector('.favorite-btn');
      if (favoriteBtn) {
        const toolId = favoriteBtn.dataset.toolId;
        if (favorites.includes(toolId)) {
          item.style.display = 'block';
        } else {
          item.style.display = 'none';
        }
      }
    });
  }
});

// 收藏功能实现
function toggleFavorite(event, toolId) {
  event.preventDefault();
  const favorites = JSON.parse(localStorage.getItem('toolFavorites') || '[]');
  const index = favorites.indexOf(toolId);
  const favoriteBtn = event.currentTarget;
  const emptyStarIcon = favoriteBtn.querySelector('.star-empty');
  const filledStarIcon = favoriteBtn.querySelector('.star-filled');
  const toolItem = favoriteBtn.closest('.tool-item');
  
  // 检查当前是否在"常用工具"分类下
  const activeCategory = document.querySelector('.category-badge.active');
  const isCommonCategory = activeCategory && activeCategory.dataset.category === 'common';

  if (index === -1) {
    // 添加到收藏
    favorites.push(toolId);
    // 切换星标显示状态
    if (emptyStarIcon && filledStarIcon) {
      emptyStarIcon.style.display = 'none';
      filledStarIcon.style.display = 'inline';
    }
  } else {
    // 从收藏中移除
    favorites.splice(index, 1);
    // 切换星标显示状态
    if (emptyStarIcon && filledStarIcon) {
      emptyStarIcon.style.display = 'inline';
      filledStarIcon.style.display = 'none';
    }
    
    // 如果在常用工具分类下，需要隐藏被取消收藏的工具
    if (isCommonCategory && toolItem) {
      toolItem.style.display = 'none';
    }
  }

  localStorage.setItem('toolFavorites', JSON.stringify(favorites));
}

// 初始化收藏状态
document.addEventListener('DOMContentLoaded', () => {
  const favorites = JSON.parse(localStorage.getItem('toolFavorites') || '[]');
  
  // 更新收藏图标状态
  favorites.forEach(id => {
    const favoriteBtn = document.querySelector(`[data-tool-id='${id}']`);
    if (favoriteBtn) {
      const emptyStarIcon = favoriteBtn.querySelector('.star-empty');
      const filledStarIcon = favoriteBtn.querySelector('.star-filled');
      if (emptyStarIcon && filledStarIcon) {
        emptyStarIcon.style.display = 'none';
        filledStarIcon.style.display = 'inline';
      }
    }
  });
  
  // 如果当前是常用工具分类，确保收藏的工具显示
  const activeCategory = document.querySelector('.category-badge.active');
  if (activeCategory && activeCategory.dataset.category === 'common') {
    const toolItems = document.querySelectorAll('.tool-item');
    toolItems.forEach(item => {
      const favoriteBtn = item.querySelector('.favorite-btn');
      if (favoriteBtn) {
        const toolId = favoriteBtn.dataset.toolId;
        if (favorites.includes(toolId)) {
          item.style.display = 'block';
        } else {
          item.style.display = 'none';
        }
      }
    });
  }
});